//
//  RECViewController.m
//  SunXi
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "RECViewController.h"

@interface RECViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSArray *drawerMarkArr; /**< 抽屉数组 */
@property(nonatomic, strong) RECview *baseView;

@end

@implementation RECViewController{
    UILabel *_playlabel;
    UIView *_playView;
    UIImageView *_playimageView;
    UIButton *_playButton;
    UIProgressView *_progreass;
    NSTimer *_time;
    UIImageView *_userimageView;
    UILabel *_userLabel;
}
- (void)dealloc
{
    /*
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(takeLabel) name:@"已登录" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(autoLabel) name:@"自动登录" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatelabel) name:@"退出登录" object:nil];
     */
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"已登录" object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"自动登录" object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"退出登录" object:nil];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.drawerMarkArr = [NSArray arrayWithObjects:NSLocalizedString(@"home", nil), NSLocalizedString(@"Radio", nil), NSLocalizedString(@"Read", nil), NSLocalizedString(@"Show", nil), NSLocalizedString(@"Mine", nil), nil];


    [self createDrawerView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playDrawerView) name:NSLocalizedString(@"Replace", nil) object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playDrawerView) name:NSLocalizedString(@"Start", nil) object:nil];
//MARK:添加登陆/注册按钮
    [self initloadbutton];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushWeb:) name:@"网页推送" object:nil];

    
}


- (void)pushWeb:(NSNotification *)obj{
    NSDictionary *dic = obj.object;
    NSLog(@"%@",dic);
    
    [[webWindow sharedInstance] show:dic];
    
}
/**
 *  添加注册登陆按钮
 */
- (void)initloadbutton{
#warning 如果有时间将iamgeView 换成button
    _userimageView = [[UIImageView alloc]init];
    CGFloat hei = HEIGHT * 0.15f;
    [SV addSubview:_userimageView];
    [_userimageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(SV).offset(hei);
        make.left.equalTo(SV).offset(40);
        make.height.and.width.mas_equalTo(40);
    }];

    _userimageView.image = [UIImage imageNamed:@"yonghu"];

    _userLabel = [[UILabel alloc]init];
    [SV addSubview:_userLabel];
    [_userLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_userimageView.mas_right).offset(20);
        make.size.mas_equalTo(CGSizeMake(200, 20));
        make.centerY.equalTo(_userimageView);
        
    }];

    if ([UserModel shareSingleton].userName.length > 0) {
        _userLabel.text = [UserModel shareSingleton].userName;
    }else{
        _userLabel.text = @"注册/登陆";
    }
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [SV addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_userLabel);
    }];
    [button addTarget:self action:@selector(loadclick:) forControlEvents:UIControlEventTouchUpInside];
//添加登陆成功的通知中心
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(takeLabel) name:@"已登录" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(autoLabel) name:@"自动登录" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatelabel) name:@"退出登录" object:nil];
    
    
}
- (void)takeLabel{
    
    [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[HomeViewController alloc] init]] animated:YES];
    [self.sideMenuViewController hideMenuViewController];
    _userLabel.text = [UserModel shareSingleton].userName;

}

- (void)autoLabel{
    _userLabel.text = [UserModel shareSingleton].userName;
}
- (void)updatelabel{
    _userLabel.text = @"注册/登陆";
}
/**
 *  按钮方法
 */
- (void)loadclick:(UIButton *)button{
    if ([UserModel shareSingleton].userName.length == 0) {
        [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[UserLoadViewController alloc] init]] animated:YES];
        [self.sideMenuViewController hideMenuViewController];

    }
}

- (void)createDrawerView {
    self.baseView = [[RECview alloc]init];
    [self.view addSubview:self.baseView];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(SV).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
   
    self.baseView.baseTableView.delegate = self;
    self.baseView.baseTableView.dataSource = self;
    self.baseView.baseTableView.opaque = NO;
    self.baseView.baseTableView.backgroundColor = [UIColor clearColor];
    self.baseView.baseTableView.backgroundView = nil;
    self.baseView.baseTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.baseView.baseTableView.bounces = NO;

    [self.baseView.baseTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
}
- (void)playDrawerView{
    //    底部电台播放器Lab
    if (!_playView) {
    _playView = [[UIView alloc] init];
    [SV addSubview:_playView];
    [_playView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(SV.mas_bottom).offset(-HEIGHT * 0.15);
        make.bottom.and.left.right.equalTo(SV);
    }];
    _playView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:.7];
    
    _playimageView = [[UIImageView alloc]init];
    [_playView addSubview:_playimageView];
    [_playimageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.top.and.equalTo(_playView);
        make.bottom.equalTo(_playView).offset(-2);
        make.width.equalTo(_playimageView.mas_height);
    }];
    
    _playlabel = [[UILabel alloc]init];
    [_playView addSubview:_playlabel];
    [_playlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_playimageView.mas_right).offset(10);
        make.right.equalTo(_playView).offset(- 60);
        make.height.mas_equalTo(20);
        make.centerY.equalTo(_playView);
    }];
    
    _playButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_playView addSubview:_playButton];
    [_playButton mas_makeConstraints:^(MASConstraintMaker *make) {
      
        make.right.equalTo(_playView).offset(-17);
        make.width.height.mas_equalTo(30);
        make.centerY.equalTo(_playView);
    }];
    [_playButton setImage:[UIImage imageNamed:@"stop.png"] forState:UIControlStateNormal];
    [_playButton addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    [_playButton setTintColor:BUTTONCOLOR];
    _progreass = [[UIProgressView alloc]initWithFrame:CGRectMake(0, HEIGHT - 2, WIDTH, 2)];
    [SV addSubview:_progreass];
    _progreass.progressTintColor = BUTTONCOLOR;
    _progreass.trackTintColor = [UIColor whiteColor];
    
    
    if (!_time) {
        _time = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timeClick) userInfo:nil repeats:YES];
    }
    }
    [self pushmusic];
    
}
- (void) clickAction:(UIButton *) button {
    if ([myplay sharePlayer].rate > 0) {
        [[myplay sharePlayer] pause];
        [button setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
    } else {
        [[myplay sharePlayer] play];
        [button setImage:[UIImage imageNamed:@"stop.png"] forState:UIControlStateNormal];
    }
}

- (void)timeClick{
    
    if ([myplay sharePlayer].rate == 0) {
        [RadioSavemodel shareSingleton].statre = NO;

        [_playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
    } else {
        [RadioSavemodel shareSingleton].statre = YES;

        [_playButton setImage:[UIImage imageNamed:@"stop.png"] forState:UIControlStateNormal];
    }
    _progreass.progress = CMTimeGetSeconds([myplay sharePlayer].currentItem.currentTime) / CMTimeGetSeconds([myplay sharePlayer].currentItem.duration);
}
- (void)pushmusic{
    [_playimageView sd_setImageWithURL:[NSURL URLWithString:[[[RadioSavemodel shareSingleton].playlistArr objectAtIndex:[RadioSavemodel shareSingleton].playNumber] coverimg]]placeholderImage:[UIImage imageNamed:@"zan"]];
    _playlabel.text = [[[RadioSavemodel shareSingleton].playlistArr objectAtIndex:[RadioSavemodel shareSingleton].playNumber] title];
}

#pragma mark tableView协议方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.drawerMarkArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    cell.textLabel.text = self.drawerMarkArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:.7];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 0:
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[HomeViewController alloc] init]] animated:YES];
            [self.sideMenuViewController hideMenuViewController];
            break;
        case 1:
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[RadioViewController alloc] init]] animated:YES];
            [self.sideMenuViewController hideMenuViewController];
            break;
        case 2  :
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[ReadingViewController alloc] init]] animated:YES];
            [self.sideMenuViewController hideMenuViewController];
            break;
        case 3:
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[MVRootViewController alloc] init]] animated:YES];
            [self.sideMenuViewController hideMenuViewController];
            break;
        case 4:
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[MyViewController alloc] init]] animated:YES];
            [self.sideMenuViewController hideMenuViewController];
            break;
        default:
            break;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
